//
//  BCBankViewController.m
//  BCVersion
//
//  Created by 吴 崧 on 14-2-25.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "BCBankViewController.h"
#define ItemSize  CGSizeMake(90,90)
#define SectionInset UIEdgeInsetsMake(0, 10, 0, 10)
#define MinimumLineSpacing 10
#define ReuseIdentifier @"BCBankIdentifier"
#import "BCBankCollectionCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "BCBankModel.h"
@interface BCBankViewController ()
@property(nonatomic,strong)NSArray *dataSource;
@end

@implementation BCBankViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.dataSource = [NSArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //init layOut
    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    layOut.itemSize = ItemSize;
    layOut.sectionInset = SectionInset;
    layOut.minimumLineSpacing = MinimumLineSpacing;
    //conf collectionView
    self.collectionView.collectionViewLayout = layOut;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    //Register CollectionCell
    UINib *nib = [UINib nibWithNibName:@"BCBankCollectionCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:ReuseIdentifier];
    
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [BCNetworkClient bankAds:0 limit:12 success:^(NSNumber *total, NSString *type, NSArray *ads) {
        self.dataSource = [BCBankModel modelsWithDataSource:ads];
        ads = nil;
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        NSString *failDesc = [NSString stringWithFormat:@"status = %d\n原因:%@",1,error];
        [UIAlertView showWithTitle:@"下载失败"
                           message:failDesc
                 cancelButtonTitle:@"确定"
                 otherButtonTitles:nil
                          tapBlock:nil];

    }];
 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
         return _dataSource.count;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
     BCBankCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentifier forIndexPath:indexPath ];
     __weak BCBankModel *model = [self.dataSource objectAtIndex:indexPath.row];
     [cell.bcbankImage setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:nil];
    
     return cell;
     
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  __weak  BCBankModel *model = [self.dataSource objectAtIndex:indexPath.row];
    self.exhibition = [[ExhibitionView alloc]initWithURL:model.url];
    [self.exhibition Show];
     
}


@end
